package com.sls.mall.order.config;

import com.sls.mall.order.rabbit.OrderConfirmCallback;
import com.sls.mall.order.rabbit.OrderReturnCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lixue
 * @Title: MyRabbitConfig
 * @ProjectName sls_mall2021
 * @Description: TODO
 * @date 2021/5/909:37
 */
@Slf4j
@Configuration
public class MyRabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AmqpAdmin amqpAdmin;

    @Bean
    public MessageConverter messageConverter()
    {
        return new Jackson2JsonMessageConverter();
    }


    @PostConstruct
    public void initRabbitTemplate()
    {
        rabbitTemplate.setConfirmCallback(new OrderConfirmCallback());

        rabbitTemplate.setReturnCallback(new OrderReturnCallback());

    }


    @Bean
    public void createExchange()
    {
        // 	public DirectExchange(
        // String name,               交换机的名字
        // boolean durable,           是否持久化
        // boolean autoDelete)        是否自动删除

        DirectExchange directExchange = new DirectExchange("ceshi-java-exchange",true,false);
        amqpAdmin.declareExchange(directExchange);

        log.info("Exchange[{}]创建成功","ceshi-java-exchange");
    }

    @Bean
    public void createDeadExchange()
    {
        // 	public DirectExchange(
        // String name,               交换机的名字
        // boolean durable,           是否持久化
        // boolean autoDelete)        是否自动删除
        DirectExchange directExchange = new DirectExchange("ceshi-java-dead-exchange",true,false);
        amqpAdmin.declareExchange(directExchange);

        log.info("Exchange[{}]创建成功","ceshi-java-dead-exchange");
    }


    @Bean
    public void createQueue(){

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "ceshi-java-dead-exchange");
        arguments.put("x-dead-letter-routing-key", "order.dead");

        // public Queue(
        // String name,              队列的名字
        // boolean durable,          是否持久化
        // boolean exclusive,        是否排他  （就是一条连接 连上了 其他就不能连接）
        // boolean autoDelete)       是否自动删除
        Queue queue = new Queue("ceshi-java-queue",true,false,false,arguments);

        amqpAdmin.declareQueue(queue);

        log.info("queue[{}]创建成功","ceshi-java-queue");
    }

    @Bean
    public void createBinding(){

        // public Binding(
        // String destination,                            目的地
        // Binding.DestinationType destinationType,       目的地类型
        // String exchange,                               交换机
        // String routingKey,                             路由键
        // @Nullable Map<String, Object> arguments)       自定义参数

        Binding binding = new Binding(
                "ceshi-java-queue",
                Binding.DestinationType.QUEUE,
                "ceshi-java-exchange",
                "ceshi.java",
                null);

        amqpAdmin.declareBinding(binding);

        log.info("Binding[{}]创建成功","ceshi-java-binging");
    }


    @Bean
    public void createDeadQueue()
    {

//		Map<String, Object> arguments = new HashMap<>();
//		arguments.put("x-dead-letter-exchange", "ceshi-java-dead-exchange");
//		arguments.put("x-dead-letter-routing-key", "order.dead");
//		arguments.put("x-message-ttl", 120000); // 仅仅用于测试，实际根据需求，通常30分钟或者15分钟

        // public Queue(
        // String name,              队列的名字
        // boolean durable,          是否持久化
        // boolean exclusive,        是否排他  （就是一条连接 连上了 其他就不能连接）
        // boolean autoDelete)       是否自动删除
        // Map<String, Object>       自定义属性
        Queue queue = new Queue("ceshi-java-dead-queue",true,false,false);

        amqpAdmin.declareQueue(queue);

        log.info("queue[{}]创建成功","ceshi-java-dead-queue");
    }

    @Bean
    public void createDeadBinding(){

        // public Binding(
        // String destination,                            目的地
        // Binding.DestinationType destinationType,       目的地类型
        // String exchange,                               交换机
        // String routingKey,                             路由键
        // @Nullable Map<String, Object> arguments)       自定义参数

        Binding binding = new Binding(
                "ceshi-java-dead-queue",
                Binding.DestinationType.QUEUE,
                "ceshi-java-dead-exchange",
                "order.dead",
                null);

        amqpAdmin.declareBinding(binding);

        log.info("Binding[{}]创建成功","ceshi-java-dead-binging");
    }




}
